<?php
session_start();

require './includes/functions.php';

if (!isset($_SESSION['user'])) {
    redirect_to("../401.php");
}

if (isset($_GET['msg'])) {
    
    
    $msg = $_GET['msg'];
    if(str_word_count($msg) == 2) {
        $msg .= "&eacute;";
    }
    
    $page = $_GET['page'];
    $couleur = $_GET['color'];
    
    echo "<link rel=\"stylesheet\" href=\"./css/screen.css\" media=\"screen\" />";
        echo "<br /><br /><h1 style=\"color: font-family: 'PT Sans Narrow'\"><b style=\"color: $couleur;\">$msg</b></h1><h4>Redirection vers la page Sponsor dans <b><span id='compteur' class='compteur'>";
        ?>

        <script language='javascript'>

            function timestamp(t)
            {
                document.getElementById("compteur").innerHTML=(t/1000);
                t-=1000;
                if(t==0)
                    self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                                    		 
                setTimeout("timestamp("+t+")",1000);
            }
            timestamp(2000);
        </script>
        <?php
        echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
    
    
}
else {

if (isset($_POST['id'])) {

    $id = $_POST['id'];
    $nom = addslashes($_POST['nom']);
    $site = $_POST['site'];
    $logo = $_POST['logo'];
    $page = "adm_sponsors.php";

    require './includes/db.php';
    echo "<link rel=\"stylesheet\" href=\"./css/screen.css\" media=\"screen\" />";

    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    $mysqli->set_charset("utf8");

    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL with MySQLI: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }



    if ($id == -1) {


        $sql = "insert into and_sponsors (nom, site, logo) values ('$nom','$site', '$logo')";
        $msg = "Sponsor ajout&eacute;";
        $msg_err = "Erreur d'ajout du sponsor !!";
    } else {

        $sql = "update and_sponsors set nom='$nom', site='$site', logo='$logo' where id =$id";
        $msg = "Sponsor modifi&eacute;";
        $msg_err = "Erreur modification du sponsor !!";
    }


    $mysqli->query($sql) or die('Problem executing update/insert query ' . $mysqli->error);


    if ($mysqli->affected_rows > 0) {
        ?>
        <script language='javascript'>
            parent.location=encodeURI("./act_sponsors.php?msg=<?php echo $msg; ?>&page=<?php echo $page; ?>&color=yellowgreen");
        </script>
        <?php
        echo "<br /><br /><h1 style=\"color: font-family: 'PT Sans Narrow'\"><b style=\"color: yellowgreen;\">$msg</b></h1><h4>Redirection vers la page Sponsor dans <b><span id='compteur' class='compteur'>";
        ?>

        <script language='javascript'>

            function timestamp(t)
            {
                document.getElementById("compteur").innerHTML=(t/1000);
                t-=1000;
                if(t==0)
                    self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                                    		 
                setTimeout("timestamp("+t+")",1000);
            }
            timestamp(2000);
        </script>
        <?php
        echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
    } else {
        ?>
        <script language='javascript'>
            parent.location=encodeURI("./act_sponsors.php?msg=<?php echo htmlentities($msg_err); ?>&page=<?php echo $page; ?>&color=rgb(230, 0, 17)");
        </script>
        <?php
        echo "<br /><br /><h1><b style=\"color: rgb(230, 0, 17);\">$msg_err</b></h1><h4>Redirection vers la page Sponsor dans <b><span id='compteur' class='compteur'>";
        ?>

        <script language='javascript'>

            function timestamp(t)
            {
                document.getElementById("compteur").innerHTML=(t/1000);
                t-=1000;
                if(t==0)
                    self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                                    		 
                setTimeout("timestamp("+t+")",1000);
            }
            timestamp(2000);
        </script>
        <?php
        echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
    }

    $mysqli->close();
} else {

    require './includes/db.php';


    if (isset($_GET['id']) && !isset($_GET['nom'])) {


        echo "<link rel=\"stylesheet\" href=\"./css/screen.css\" media=\"screen\" />";

        $id = $_GET['id'];

        $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
        $mysqli->set_charset("utf8");

        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL with MySQLI: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        }


        $sql = "delete from and_sponsors where id = " . $id;

        $mysqli->query($sql) or die('Problem deleting' . $mysqli->errno);


        if ($mysqli->affected_rows > 0) {

            echo "<br /><br /><h1 style=\"color: font-family: 'PT Sans Narrow'\"><b style=\"color: yellowgreen;\">Sponsor supprim&eacute;</b></h1><h4>Redirection vers la page Sponsor dans <b><span id='compteur' class='compteur'>";
            ?>

            <script language='javascript'>

                function timestamp(t)
                {
                    document.getElementById("compteur").innerHTML=(t/1000);
                    t-=1000;
                    if(t==0)
                        history.back();
                                                                                                                                                                                                                                                                                                                            		 
                    setTimeout("timestamp("+t+")",1000);
                }
                window.onload = timestamp(2000);
            </script>
            <?php
            echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
        } else {

            echo "<br /><br /><h1><b style=\"color: rgb(230, 0, 17);\">Erreur de suppression de la publication</b></h1><h4>Redirection vers la page actualit&eacute dans <b><span id='compteur' class='compteur'>";
            ?>

            <script language='javascript'>

                function timestamp(t)
                {
                    document.getElementById("compteur").innerHTML=(t/1000);
                    t-=1000;
                    if(t==0)
                        history.back();
                                                                                                                                                                                                                                                                                                                            		 
                    setTimeout("timestamp("+t+")",1000);
                }
                window.onload = timestamp(2000);
            </script>
            <?php
            echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
        }



        $mysqli->close();
    } else {




        if (!isset($_GET['id'])) {
            $titre_page = "Ajouter un sponsor";
            $bouton = "Ajouter";

            $id = -1;
            $nom = "";
            $site = "";
            $logo = "";
        }

        if (isset($_GET['nom'])) {

            $titre_page = "Modifier un sponsor";
            $bouton = "Modifier";

            $id = $_GET['id'];
            $nom = stripslashes($_GET['nom']);
            $site = $_GET['site'];
            $logo = $_GET['logo'];
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en">
            <head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW" />
                <!--

        Base template (without user's data) checked by http://validator.w3.org : "This page is valid XHTML 1.0 Transitional"
                -->
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
                <title>ISIMG Android</title>



                <link href="./css/screen.css" rel="stylesheet" media="screen" />


                <link rel="stylesheet" href="./css/style.css" type="text/css" media="screen" />
                <!--[if IE 6]><link rel="stylesheet" href="./css/style.ie6.css" type="text/css" media="screen" /><![endif]-->
                <!--[if IE 7]><link rel="stylesheet" href="./css/style.ie7.css" type="text/css" media="screen" /><![endif]-->

                <style>

                    .henislayout-cell, .heniscontent-layout-row
                    {
                        background-color: Transparent;
                        vertical-align: top;
                        text-align: left;
                        border: none;

                        padding:0;
                    }
                    .henisarticle td
                    {
                        padding: 5px;
                        border: solid 0px #616161;
                        vertical-align: top;
                        text-align: left;
                    }
                    .preview
                    {
                        width:200px;
                        border:solid 1px #dedede;
                        padding:10px;
                    }

                </style>




                <script type="text/javascript">
                                                                                                                                                                                                                
                    function verif_formulaire(e)
                    {
                        //                var returnval=emailfilter.test(e.value)
                        if(document.formulaire.nom.value == "")  {
                            alert("Veuillez saisir le nom du sponsor!");
                            document.formulaire.nom.focus();
                            return false;
                        }
                                                                                                                         
                                                                                                                                                                                                                
                                                                                                                                                                                                          
                    }


                </script>



                <script type="text/javascript" src="./js/jquery.js"></script>
                <script type="text/javascript" src="./js/script.js"></script>



                                                <!--<script type="text/javascript" src="./js/upload/jquery.min.js"></script>-->
                <script type="text/javascript" src="./js/jquery.form.js"></script>


                <script type="text/javascript" >
                    $(document).ready(function() { 
                                                                    		
                        $('#logo1').live('change', function() { 
                            $("#preview").html('');
                            $("#preview").html('<img src="./images/ajax-loader.gif" alt="Téléchargement en cours ...."/>');
                            $("#form1").ajaxForm({
                                target: '#preview'
                            }).submit();
                                                  
                                                   
                        });
                    }); 
                                                                        
                                            
                </script>


            </head>
            <body>

                <div id="henispage-background-glare">
                    <div id="henispage-background-glare-image">
                        <div id="henismain">
                            <div class="henisheader">
                                <div class="henisheader-center">
                                    <div class="henisheader-jpeg"></div>
                                </div>
                                <div class="henisheader-wrapper">
                                    <div class="henisheader-inner">
                                        <div class="henisheaderobject"></div>
                                        <div class="henislogo">
                                            <h1 id="name-text" class="henislogo-name"><a href="./index.php">Administration du site</a></h1>
                                            <h2 id="slogan-text" class="henislogo-text">ISIMG Android</h2>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="henisnav">
                                <div class="henisnav-l"></div>
                                <div class="henisnav-r"></div>
                                <div class="henisnav-wrapper">
                                    <div class="henisnav-inner">
                                        <ul class="henismenu">
                                            <li><a href="./index.php"><span class="l"> </span><span class="r"> </span><span class="t">Accueil</span></a></li>
                                            <li><a href="./adm_actualites.php"><span class="l"> </span><span class="r"> </span><span class="t">Actualités</span></a></li>
                                            <li><a href="./adm_sponsors.php" class="active"><span class="l"> </span><span class="r"> </span><span class="t">Sponsors</span></a></li>
                                            <li><a href="./adm_comite_pilotage.php"><span class="l"> </span><span class="r"> </span><span class="t">Comité de pilotage</span></a></li>
                                            <li><a href="./adm_comite_organisation.php"><span class="l"> </span><span class="r"> </span><span class="t">Comité d'organisation</span></a></li>
                                            <li><a href="./adm_participants.php"><span class="l"> </span><span class="r"> </span><span class="t">Participants</span></a></li>
                                            <li><a href="./adm-auth/act_authentification.php?action=logout"><span class="l"> </span><span class="r"> </span><span class="t">Déconnexion</span></a></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="henissheet">
                                <div class="henissheet-tl"></div>
                                <div class="henissheet-tr"></div>
                                <div class="henissheet-bl"></div>
                                <div class="henissheet-br"></div>
                                <div class="henissheet-tc"></div>
                                <div class="henissheet-bc"></div>
                                <div class="henissheet-cl"></div>
                                <div class="henissheet-cr"></div>
                                <div class="henissheet-cc"></div>
                                <div class="henissheet-body">
                                    <div class="heniscontent-layout">
                                        <div class="heniscontent-layout-row">
                                            <div class="henislayout-cell heniscontent">
                                                <div class="henispost">
                                                    <div class="henispost-tl"></div>
                                                    <div class="henispost-tr"></div>
                                                    <div class="henispost-bl"></div>
                                                    <div class="henispost-br"></div>
                                                    <div class="henispost-tc"></div>
                                                    <div class="henispost-bc"></div>
                                                    <div class="henispost-cl"></div>
                                                    <div class="henispost-cr"></div>
                                                    <div class="henispost-cc"></div>
                                                    <div class="henispost-body">
                                                        <div class="henispost-inner henisarticle">
                                                            <h2 class="henispostheader"><?php echo $titre_page; ?></h2><br /><br />
                                                            <div class="henispostcontent">
                                                                <div class="aligncenter">
                                                                    <table>
                                                                        <tbody>
                                                                            <tr>
                                                                                <td>
                                                                                    <form id="form1" name="formulaire" enctype="multipart/form-data" action="./ajax_image.php" method="post">	

                                                                                        <fieldset>
                                                                                            <p class="first">
                                                                                                <label for="nom">Organisation</label>
                                                                                                <input type="text" name="nom" id="nom" size="100" value="<?php echo $nom; ?>" />
                                                                                            </p>

                                                                                            <p class="first">



                                                                                                <label for="site">Site web</label>
                                                                                                <input type="text" name="site" id="site" size="100" value="<?php echo $site; ?>" />
                                                                                            </p>

                                                                                            <p>	
                                                                                                <br />
                                                                                                <label>Logo</label>
                                                                                            </p>

                                                                                            <p>

                                                                                                <input type="file" name="logo1" id="logo1" value="<?php echo $logo; ?>" />

                                                                                            </p>

                                                                                        </fieldset>
                                                                                        <fieldset>


                                                                                            <div id='preview'>

                                                                                                <img src='<?php echo $logo; ?>' class='preview' />
                                                                                                <input type='hidden' name='logo' value='<?php echo $logo; ?>' />

                                                                                            </div>



                                                                                        </fieldset>


                                                                                        <input type="hidden" name="id" value="<?php echo $id; ?>" />

                                                                                        <p class="submit">
                                                                                            <button type="submit" onClick="document.formulaire.action ='./act_sponsors.php';"><?php echo $bouton; ?></button>
                                                                                        </p>



                                                                                    </form>
                                                                                </td>
                                                                            </tr>
                                                                        </tbody>
                                                                    </table>
                                                                </div>
                                                            </div>
                                                            <div class="cleared"></div>
                                                        </div>
                                                        <div class="cleared"></div>
                                                    </div>
                                                </div>
                                                <div class="cleared"></div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="cleared"></div>
                                    <div class="cleared"></div>
                                </div>
                            </div>
                            <div class="henisfooter">
                                <div class="henisfooter-t"></div>
                                <div class="henisfooter-b"></div>
                                <div class="henisfooter-body">
                                    <div class="henisfooter-text">
                                        <div class="cleared"></div>
                                        <p class="henispage-footer">Auteur : <a href="http://www.isimg.rnu.tn/fra/profil/heni_saber">henis</a>.</p>
                                    </div>
                                    <div class="cleared"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </body>
        </html>
        <?php
    }
}
}
?>